package yxy.game.pm2.active.module;

import org.joda.time.DateTime;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actprivilege;
import yxy.cherry.data.bean.vipdata;
import yxy.game.pm2.active.ActiveUtil;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.tip.TipHandle;
import yxy.game.pm2.module.tip.TipType;

import java.util.ArrayList;

public class ActiveModule_月卡 extends PlayerModule {

    // bean -----------------------------------------
    public static final int acttype = 102;

    public static final int monthcard1 = 1; // 黄金月卡
    public static final int monthcard2 = 2; // 尊贵月卡
    public static final int lengthoftime = 30; // 持续时间 单位 天

    // 黄金月卡
    private int mc1payexp;
    private DateTime mc1starttime; // 月卡开始时间
    private DateTime mc1endtime; // 月卡结束时间
    private DateTime mc1updatetime; // 月卡领取更新时间
    private int mc1getdays; // 月卡已经领取的天数
    // 尊贵月卡
    private int mc2payexp;
    private DateTime mc2starttime; // 月卡开始时间
    private DateTime mc2endtime; // 月卡结束时间
    private DateTime mc2updatetime; // 月卡领取更新时间
    private int mc2getdays; // 月卡已经领取的天数

    private DateTime mcupdatetime; // 月卡vip每日奖励领取更新时间

    public int getMc1payexp() {
        return mc1payexp;
    }

    public void setMc1payexp(int mc1payexp) {
        this.mc1payexp = mc1payexp;
    }

    public DateTime getMc1starttime() {
        return mc1starttime;
    }

    public void setMc1starttime(DateTime mc1starttime) {
        this.mc1starttime = mc1starttime;
    }

    public DateTime getMc1endtime() {
        return mc1endtime;
    }

    public void setMc1endtime(DateTime mc1endtime) {
        this.mc1endtime = mc1endtime;
    }

    public DateTime getMc1updatetime() {
        return mc1updatetime;
    }

    public void setMc1updatetime(DateTime mc1updatetime) {
        this.mc1updatetime = mc1updatetime;
    }

    public int getMc1getdays() {
        return mc1getdays;
    }

    public void setMc1getdays(int mc1getdays) {
        this.mc1getdays = mc1getdays;
    }

    public int getMc2payexp() {
        return mc2payexp;
    }

    public void setMc2payexp(int mc2payexp) {
        this.mc2payexp = mc2payexp;
    }

    public DateTime getMc2starttime() {
        return mc2starttime;
    }

    public void setMc2starttime(DateTime mc2starttime) {
        this.mc2starttime = mc2starttime;
    }

    public DateTime getMc2endtime() {
        return mc2endtime;
    }

    public void setMc2endtime(DateTime mc2endtime) {
        this.mc2endtime = mc2endtime;
    }

    public DateTime getMc2updatetime() {
        return mc2updatetime;
    }

    public void setMc2updatetime(DateTime mc2updatetime) {
        this.mc2updatetime = mc2updatetime;
    }

    public int getMc2getdays() {
        return mc2getdays;
    }

    public void setMc2getdays(int mc2getdays) {
        this.mc2getdays = mc2getdays;
    }

    public DateTime getMcupdatetime() {
        return mcupdatetime;
    }

    public void setMcupdatetime(DateTime mcupdatetime) {
        this.mcupdatetime = mcupdatetime;
    }

    // func ------------------------------------------

//	public static final int canget = 1;
//	public static final int hadget = 2;
//	public static final int cannotget = 3;

    /**
     * 添加经验
     *
     * @param addvipexp
     */
    public void addvipexp(int addvipexp) {
        DateTime now = DateTime.now();

        DateTime end1 = getMc1endtime();
        int ne1 = ActiveUtil.days(end1, now);

        int vipexp1 = getMc1payexp() + addvipexp;
        if (ne1 >= 1) {
            actprivilege ap = ActDataCenter.getData(monthcard1, actprivilege.class, getPlayerCode());

            if (vipexp1 >= ap.getCondition()) {
                DateTime start = DateTime.now().withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
                DateTime end = start.plusDays(lengthoftime);

                setMc1starttime(start);
                setMc1endtime(end);
                setMc1getdays(0);
                setMc1updatetime(new DateTime(0));
            }
        }
        setMc1payexp(vipexp1);
//		this.set();

        DateTime end2 = getMc2endtime();
        int ne2 = ActiveUtil.days(end2, now);

        int vipexp2 = getMc2payexp() + addvipexp;
        if (ne2 >= 1) {
            actprivilege ap = ActDataCenter.getData(monthcard2, actprivilege.class, getPlayerCode());

            if (vipexp2 >= ap.getCondition()) {
                DateTime start = DateTime.now().withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).withMillisOfSecond(0);
                DateTime end = start.plusDays(lengthoftime);

                setMc2starttime(start);
                setMc2endtime(end);
                setMc2getdays(0);
                setMc2updatetime(new DateTime(0));
            }

        }
        setMc2payexp(vipexp2);
        this.set();
    }

    /**
     * 初始化
     *
     * @param playerCode
     * @return
     */
    public static ActiveModule_月卡 create(String playerCode) {
        ActiveModule_月卡 am = new ActiveModule_月卡();

        am.setPlayerCode(playerCode);
        am.setMc1payexp(0);
        am.setMc1updatetime(new DateTime(0));
        am.setMc1starttime(new DateTime(0));
        am.setMc1getdays(0);
        am.setMc1endtime(new DateTime(0));
        am.setMc2payexp(0);
        am.setMc2updatetime(new DateTime(0));
        am.setMc2starttime(new DateTime(0));
        am.setMc2getdays(0);
        am.setMc2endtime(new DateTime(0));
        am.setMcupdatetime(new DateTime(0));

        am.set();
        return am;
    }

    /**
     * 处理领取奖励
     *
     * @param group
     * @param cur
     */
    public void handledrawreward(int group, Currency cur) {
        if (group == monthcard1) {
            setMc1updatetime(DateTime.now());
            setMc1getdays(getMc1getdays() + 1);
            this.set();
        }

        if (group == monthcard2) {
            setMc2updatetime(DateTime.now());
            setMc2getdays(getMc2getdays() + 1);
            this.set();
        }

        CurrencyHandle ch = new CurrencyHandle();
        ch.addCurrency(getPlayerCode(), CCReason.活动_月卡, cur);
    }

    /**
     * 能否领取月卡每日奖励
     *
     * @param group 月卡组
     * @return
     */
    public int canmcreward(int group) {
        DateTime nowdt = DateTime.now();

        if (group == monthcard1) {
            DateTime enddt = getMc1endtime();
            if (ActiveUtil.days(nowdt, enddt) >= 0) {
                DateTime updt = getMc1updatetime();
                if (ActiveUtil.days(updt, nowdt) >= 1) {
                    return DrawStatus.canget.getStatus();
                } else {
                    return DrawStatus.hadget.getStatus();
                }
            }
        }

        if (group == monthcard2) {
            DateTime enddt = getMc2endtime();
            if (ActiveUtil.days(nowdt, enddt) >= 0) {
                DateTime updt = getMc2updatetime();
                if (ActiveUtil.days(updt, nowdt) >= 1) {
                    return DrawStatus.canget.getStatus();
                } else {
                    return DrawStatus.hadget.getStatus();
                }
            }
        }

        return DrawStatus.cannotget.getStatus();
    }

    /**
     * 获取月卡每日奖励
     *
     * @param group
     * @return
     */
    public Currency mcreward(int group) {
        if (group == monthcard1) {
            actprivilege ap = ActDataCenter.getData(monthcard1, actprivilege.class, getPlayerCode());
            if (ap == null) {
                return null;
            }
            Currency cur = Currency.create(ap.getRewardtype(), ap.getRewardid(), ap.getRewardnum());
            return cur;
        }

        if (group == monthcard2) {
            actprivilege ap = ActDataCenter.getData(monthcard2, actprivilege.class, getPlayerCode());
            if (ap == null) {
                return null;
            }
            Currency cur = Currency.create(ap.getRewardtype(), ap.getRewardid(), ap.getRewardnum());
            return cur;
        }

        return null;
    }

    static public int cangetvipmcreward(String pcode) {
        ActiveModule_月卡 am = get(pcode, false);
        return am.cangetvipmcreward();
    }

    /**
     * 能否领取月卡vip奖励
     *
     * @return
     */
    public int cangetvipmcreward() {
        PlayerBean playerBean = PlayerBean.get(getPlayerCode());
        int viplv = playerBean.getViplevel();

        vipdata vd = DataCenter.getData(viplv, vipdata.class);
        ArrayList<Currency> curs = (ArrayList<Currency>) Currency.create(vd.getRewardtype(), vd.getRewardid(), vd.getRewardnum());
        if (curs == null || curs.isEmpty()) {
            return DrawStatus.cannotget.getStatus();
        }

        DateTime nowdt = DateTime.now();
        DateTime vmrdt = getMcupdatetime();
        if (ActiveUtil.days(nowdt, getMc1endtime()) < 0 && ActiveUtil.days(nowdt, getMc2endtime()) < 0) {
            return DrawStatus.cannotget.getStatus();
        }
        if (ActiveUtil.days(vmrdt, nowdt) == 0) {
            return DrawStatus.hadget.getStatus();
        }
        return DrawStatus.canget.getStatus();
    }

    /**
     * 更新月卡vip每日奖励领取时间
     */
    public void updateviprewardtime() {
        setMcupdatetime(DateTime.now());
        this.set();
    }

    /**
     * 获取月卡vip每日奖励
     *
     * @return
     */
    public ArrayList<Currency> handlevipreward() {
        PlayerBean playerBean = PlayerBean.get(getPlayerCode());
        int viplv = playerBean.getViplevel();
        vipdata vd = DataCenter.getData(viplv, vipdata.class);

        ArrayList<Currency> curs = (ArrayList<Currency>) Currency.create(vd.getRewardtype(), vd.getRewardid(), vd.getRewardnum());
        return curs;
    }

    /**
     * 刷新
     */
    private void update() {    //TODO
        DateTime now = DateTime.now();

        DateTime end1 = getMc1endtime();
        int ne1 = ActiveUtil.days(end1, now);

        if (ne1 >= 1 && !end1.isEqual(new DateTime(0))) {
            setMc1payexp(0);
            setMc1updatetime(new DateTime(0));
            setMc1starttime(new DateTime(0));
            setMc1getdays(0);
            setMc1endtime(new DateTime(0));
            this.set();
        }

        DateTime end2 = getMc2endtime();
        int ne2 = ActiveUtil.days(end2, now);

        if (ne2 >= 1 && !end2.isEqual(new DateTime(0))) {
            setMc2payexp(0);
            setMc2updatetime(new DateTime(0));
            setMc2starttime(new DateTime(0));
            setMc2getdays(0);
            setMc2endtime(new DateTime(0));
            this.set();
        }
    }

    // redis -----------------------------------------

    static private String key(String pcode) {
        return String.format("PLAYER:%s:ACTIVE:%S", pcode, acttype);
    }

    public void set() {
        try {
            setv(key(getPlayerCode()), this);
            check();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    static public ActiveModule_月卡 get(String playerCode, Boolean isNewPlayer) {
        try {
            ActiveModule_月卡 module = getv(key(playerCode));
            if (module == null) {
                //新用户不需要去查数据库
                if (!isNewPlayer) {
                    module = MySQLCacheDAO.getV(key(playerCode), ActiveModule_月卡.class);
                    if (module != null) {
                        module.set();
                    }//同步到redis
                }

                if (module == null) {
                    module = create(playerCode);
                }
                if (module == null) {
                    return null;
                }
            }

            module.update();
            module.check();
            return module;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查看是否有月卡可领取奖励 控制红点
     */
    public void check() {
        new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.开启);
        {
            if (canmcreward(monthcard1) == DrawStatus.canget.getStatus() || canmcreward(monthcard2) == DrawStatus.canget.getStatus()) {
                new TipHandle().updateTip(getPlayerCode(), TipType.活动_月卡奖励, true);
            } else {
                new TipHandle().updateTip(getPlayerCode(), TipType.活动_月卡奖励, false);
            }
        }

        {
            if (cangetvipmcreward() == DrawStatus.canget.getStatus()) {
                new TipHandle().updateTip(getPlayerCode(), TipType.活动_月卡vip奖励, true);
            } else {
                new TipHandle().updateTip(getPlayerCode(), TipType.活动_月卡vip奖励, false);
            }
        }
    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            String key = key(playerCode);
            if (touchBucket(key).isExists()) {
                ActiveModule_月卡 activeModule_月卡 = getv(key(playerCode));
                MySQLCacheDAO.setV(key, activeModule_月卡);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }
    // lock==========================================================================

    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getPlayerCode()));
    }
}
